package com.vote.vs.service.impl;

import com.vote.vs.dao.OptionMapper;
import com.vote.vs.dao.SubjectMapper;
import com.vote.vs.entity.Option;
import com.vote.vs.entity.Subject;
import com.vote.vs.entity.SubjectDto;
import com.vote.vs.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private OptionMapper optionMapper;

    @Override
    public List<SubjectDto> subjectList() {
        return subjectMapper.subjectList();
    }

    @Override
    public Integer insertSubject(Subject subject, String[] options) {

        int id = (int) new Date().getTime();
        subject.setVsId(id);
        System.out.println("~~~~~~~~~~subject:" + subject);
        int result = subjectMapper.insert(subject);

        Option option = new Option();
        option.setVsId(id);
        for (int i = 0; i < options.length; i++) {
            option.setVoOption(options[i]);
            option.setVoOrder(i);
            optionMapper.insert(option);
        }
        System.out.println("~~~~~~~~~~Option:" + option);
        return result;
    }
}
